package com.buba.system.pojo;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class Page<T> implements Serializable {
    // 当前页
    private Long pageNow;
    // 每页条数
    private Long pageCount;
    // 总页数
    private Long pageTotal;
    // 总条数
    private Long count;
    // sql语句中的起始下标
    private Long pageIndex;
    private List<T> list;

    /**
     * 得加无参构造器，否则分页加载不出来
     */
    public Page(){

    }

    public Page(Integer pageNow) {
        this(pageNow, 5);
    }

    public Page(long pageNow, long pageCount) {
        if (pageNow < 1)
            pageNow = 1;
        this.pageNow = pageNow;
        if (pageCount < 5)
            pageCount = 5;
        this.pageCount = pageCount;
        this.pageIndex = (pageNow - 1) * pageCount;
    }

    /**
     * 根据总条数计算总页数
     */
    public void computePageTotal(Long count) {
        this.count = count;
        // 计算总页数
        if (count % pageCount == 0)
            this.pageTotal = count / pageCount;
        else
            this.pageTotal = (count / pageCount) + 1;
    }
}
